class Solution:
    def maximumOr(self, nums: List[int], k: int) -> int:
        cnt = Counter()
        for x in nums:
            for i in range(30):
                if x >> i & 1:
                    cnt[i] += 1
        
        def calc(x: int) -> int:
            for i in range(30):
                if x >> i & 1:
                    cnt[i] -= 1
                    cnt[i + k] += 1
            
            res = sum(1 << i for i, v in cnt.items() if v)
            for i in range(30):
                if x >> i & 1:
                    cnt[i] += 1
                    cnt[i + k] -= 1
            return res

        return max(calc(x) for x in nums)
        